package com.hutao.manage.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Map;

public class FileUtil {

	public static boolean createOrEditFile(String data,String fileDir,String fileName) throws IOException{
		File destFile = new File(fileDir,fileName);
		if(!destFile.exists()){
			File destDir = new File(fileDir);
			if(!destDir.exists()){
				destDir.mkdirs();
			}
			destFile.createNewFile();
		}
		
		FileOutputStream fileOutputStream=new FileOutputStream(destFile);  
        OutputStreamWriter outputWriter=new OutputStreamWriter(fileOutputStream,"UTF-8");  
        outputWriter.write(data);
         
        outputWriter.close();  
        fileOutputStream.close();  

       
		return true;
	}
	
	public static Map<String, Object> readLineFile(String filePath, String fileName) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();

		try {
			File destFile = new File(filePath, fileName);
			String jsonStr = null;
			if (destFile.exists()) {
				// FileReader fr = new FileReader(destFile);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(new FileInputStream(filePath + fileName), "UTF-8"));
				jsonStr = br.readLine();
				jsonMap = JsonUtil.jsonToPojo(jsonStr, Map.class);

			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		return jsonMap;
	}
	
	/**
	 * 判断文件是否存在
	 * @param filePath
	 * @return
	 */
	public  static boolean fileIsExists(String filePath) {
		return  new File(filePath).exists()?true:false;
	}
	
	
	
	/**
	 * 文件复制
	 * @param srcPath 源文件路径
	 * @param targetPath 复制后存放路径
	 * @throws Exception
	 */
	public static void copyFile(String srcPath, String targetPath,String fileName) throws Exception {
		File srcFile = new File(srcPath);
		File target = new File(targetPath);
		if (!srcFile.exists()) {
			throw new Exception("文件不存在！");
		}
		if (!srcFile.isFile()) {
			throw new Exception("不是文件！");
		}
		//判断目标路径是否是目录
		if (!target.isDirectory()) {
			throw new Exception("文件路径不存在！");
		}

		
		String newFileName = targetPath + File.separator + fileName;
		File targetFile = new File(newFileName);
		FileInputStream in = null;
		FileOutputStream out = null;
		try {
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(targetFile);
			//从in中批量读取字节，放入到buf这个字节数组中，
			// 从第0个位置开始放，最多放buf.length个 返回的是读到的字节的个数
			byte[] buf = new byte[8 * 1024];
			int len = 0;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try{
				if(in != null){
				  in.close();
				}
			}catch(Exception e){
				 System.out.println("关闭输入流错误！");
			}
			try{
				if(out != null){
					out.close();
				}
			}catch(Exception e){
				System.out.println("关闭输出流错误！");
			}
		}

	}

	
	public static void main(String[] args) throws Exception {
		copyFile("D:\\publics\\video\\2021-02-23\\3343a98e766c40109d6e33fff742be1e.mp4","D:\\publics\\video\\2021-02-23\\","99999.mp4");
		
	}
	
	
}
